package com.icebergteam.warfacewidget.wiki.data;

import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.OnTaskCompleted;
import com.icebergteam.warfacewidget.core.Parser;
import com.icebergteam.warfacewidget.core.RequestTask;
import com.icebergteam.warfacewidget.core.asynctaskmanager.IHandleMessage;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;
import com.icebergteam.warfacewidget.wiki.ui.WikiActivity;
import com.icebergteam.warfacewidget.R;

	public class WikiWidgetRequestTask extends RequestTask implements IHandleMessage{

		public static final String className = WikiWidgetRequestTask.class.getCanonicalName();
		
		private WikiWidgetParser wikiWidgetParser;
		private WikiDownloadTask wikiDownloadTask;
		protected int stateRequest;
		

		public WikiWidgetRequestTask(OnTaskCompleted onTaskCompleted, Context context, Intent intent) {
			super(onTaskCompleted, context, intent);
			
			 this.wikiWidgetParser = new WikiWidgetParser();
			 this.wikiDownloadTask = new WikiDownloadTask(null, this, WikiActivity.homeWikiHtmlPage, true, false);
		}

		
		@Override
		protected Integer doInBackground(Void... paramas) {
			try {
				boolean update = !this.intent.getExtras().getBoolean(Parser.KEY_INTENT_RESTORE);
				this.wikiDownloadTask.setUpdateWikiHtml(update);
				this.wikiDownloadTask.getHtml(WikiActivity.homeWikiHtmlPage).getSecond();
					
			} catch (Exception e) {
				
				LogManager.LogEA(WikiWidgetRequestTask.className, ViewUtils.getErrorMessage(e));
			
				
					
			}
			return this.wikiWidgetParser.getStateRequest();
		}

		@Override
		protected void onPostExecute(Integer result) {
		
			this.intent.putExtra(WidgetProvider.TYPE_LIST, WidgetProvider.WIKI_TYPE_LIST);
			
			WidgetProvider.setTypeCurrentService(WidgetProvider.WIKI_TYPE_LIST);
			
			
			super.onPostExecute(result);
		}
		
		public static void setWidgetState(RemoteViews view, int result){
							
				view.setEmptyView(R.id.gridview, R.id.emptyTextView);
				view.setTextViewText(R.id.emptyTextView,  (result == (Parser.REQUEST_FAILED ) || result == Parser.DB_INVALID) ? AppManager.getContext().getResources().getText(R.string.errorTextView ): AppManager.getContext().getResources().getText(R.string.emptyTextView) );
//				
//				try {
//					Date lastUpdateDate = AppManager.getDatabase().getNewsTable().getLastUpdate();
//					String date = String.format(
//							
//							"%s", ((lastUpdateDate.equals(new Date(0))) ? "": TimeDateUtils.getDateByLocale(lastUpdateDate, new Locale(AppManager.getLocaleManager().getCurrentUILangId()), "dd.MM.yy" /*HH:mm*//*"dd MMM yyyy HH:mm:ss"*/)));
//			
//					view.setTextViewText(R.id.widget_control_panel_date_TextView, date);
//					
//					LogManager.LogV(WikiWidgetRequestTask.className, LogManager.getExecuteMethodName(), result == Parser.REQUEST_COMPLETED,  result == (Parser.REQUEST_FAILED ), view);
//				} catch (Exception e) {
//					LogManager.LogE(WikiWidgetRequestTask.className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
//					e.printStackTrace();
//				}
			}


		@Override
		public void handleMessage(String title, String string) {
			// TODO Auto-generated method stub
			
		}


		@Override
		public void handleError(String title, String string) {
			// TODO Auto-generated method stub
			this.stateRequest = Parser.REQUEST_FAILED;
		}
	}